package com.zmn.mcc.services.impl.staff.staffcontract;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.model.entity.staff.dingtalk.StaffDingTalk;
import com.zmn.mcc.model.entity.staff.staffcontract.StaffContract;
import com.zmn.mcc.model.entity.staff.staffcontract.StaffContractQuery;
import com.zmn.mcc.persistence.interfaces.staff.staffcontract.StaffContractDAO;
import com.zmn.mcc.services.interfaces.staff.staffcontract.StaffContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 描述: 员工合同信息基础接口实现类
 *
 * @author tanghongjun
 * @since 2020-05-14 10:21:58
 **/
@Slf4j
@Service
public class StaffContractServiceImpl implements StaffContractService {

    private static final String TAG = "员工合同信息基础服务接口";

    @Resource
    private StaffContractDAO staffContractDao;

    /**
     * 描述: 根据id查询数据
     *
     * @param contractId id
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public StaffContract findById(Integer contractId) {

        if (Objects.isNull(contractId)) {
            log.error("[{}],根据id查询数据contractId为null", TAG);
            return null;
        }
        return staffContractDao.findById(contractId);
    }

    @Override
    public StaffContract findByStaffId(Integer staffId) {
        if (Objects.isNull(staffId)) {
            log.error("[{}],根据staffId查询数据为null", TAG);
            return null;
        }
        return staffContractDao.findByStaffId(staffId);
    }

    /**
     * 描述: 按条件查询数据 支持分页
     *
     * @param query 查询对象
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public List<StaffContract> listPageByQuery(StaffContractQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return staffContractDao.listPageByQuery(query);
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query 查询对象
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public List<StaffContract> listByQuery(StaffContractQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return staffContractDao.listByQuery(query);
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query 查询对象
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public Integer countByQuery(StaffContractQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return staffContractDao.countByQuery(query);
    }


    /**
     * 描述: 根据id修改数据 id为必传
     *
     * @param staffContract 待修改数据
     * @return java.lang.Integer
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public Integer updateStaffContractByKey(StaffContract staffContract) {

        if (Objects.isNull(staffContract.getContractId())) {
            log.error("[{}],根据id修改数据,staffContract为null", TAG);
            return null;
        }
        return staffContractDao.updateStaffContractByKey(staffContract);
    }

    @Override
    public Integer synchroByDingTalk(Integer staffId, StaffDingTalk staffDingTalk) {
        if (Objects.isNull(staffId)) {
            log.error("员工ID不能为空", TAG);
            return null;
        }
        StaffContract staffContract = staffContractDao.findByStaffId(staffId);
        if (Objects.isNull(staffContract)) {
            staffContract = new StaffContract();
            staffContract.setStaffId(staffId);
        }
        if (Objects.isNull(staffContract.getFristContractTime()) && Objects.nonNull(staffDingTalk.getFristContractTime())) {
            staffContract.setFristContractTime(staffDingTalk.getFristContractTime());
        }
        if (Objects.isNull(staffContract.getFristContractEndTime()) && Objects.nonNull(staffDingTalk.getFristContractEndTime())) {
            staffContract.setFristContractEndTime(staffDingTalk.getFristContractEndTime());
        }
        if (Objects.isNull(staffContract.getContractTimeNow()) && Objects.nonNull(staffDingTalk.getContractTimeNow())) {
            staffContract.setContractTimeNow(staffDingTalk.getContractTimeNow());
        }
        if (Objects.isNull(staffContract.getContractEndTimeNow()) && Objects.nonNull(staffDingTalk.getContractEndTimeNow())) {
            staffContract.setContractEndTimeNow(staffDingTalk.getContractEndTimeNow());
        }
        if (Objects.isNull(staffContract.getContractTimes()) && Objects.nonNull(staffDingTalk.getContractTimes())) {
            staffContract.setContractTimes(staffDingTalk.getContractTimes());
        }
        if (NumberUtil.isNullOrZero(staffContract.getContractId())) {
            return staffContractDao.insert(staffContract);
        }
        return staffContractDao.updateStaffContractByKey(staffContract);
    }

    /**
     * 描述: 新增数据
     *
     * @param staffContract 待新增数据
     * @return java.lang.Integer 主键key
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public Integer insert(StaffContract staffContract) {

        if (Objects.isNull(staffContract)) {
            log.error("[{}],新增数据,staffContract为null", TAG);
            return null;
        }

        return staffContractDao.insert(staffContract);
    }

    /**
     * 描述: 根据主键删除数据
     *
     * @param contractId
     * @return: 执行结果码
     * @auther: tanghongjun
     * @since: 2020-05-14 10:21:58
     */
    @Override
    public Integer deleteByKey(Integer contractId) {

        if (Objects.isNull(contractId)) {
            log.error("[{}],根据id删除数据,contractId为null", TAG);
            return null;
        }
        return staffContractDao.deleteByKey(contractId);
    }

}